package com.zx.framework.plat.user.controller;

import com.zx.framework.core.env.CONST;
import com.zx.framework.core.pagination.Pagination;
import com.zx.framework.core.util.DatetimeUtils;
import com.zx.framework.core.util.RandomUtil;
import com.zx.framework.plat.param.po.Param;
import com.zx.framework.plat.user.po.User;
import com.zx.framework.plat.user.service.UserService;
import com.zx.framework.web.controller.BaseController;
import com.zx.framework.web.exception.AjaxException;
import com.zx.framework.web.exception.UIException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @InitBinder("user")
    public void userBinder(WebDataBinder binder) throws Exception {
        binder.setFieldDefaultPrefix("user.");
    }

    /**
     * 分页查询方法
     *
     * @param user       查询模板
     * @param pagination 分页
     * @return 分页模板路径
     */
    @RequestMapping("/page")
    public String query(User user, Pagination<User> pagination, Model model) {
        try {
            model.addAttribute("user", user);
            model.addAttribute("pagination", userService.pageQuery(user, pagination.getPage(), pagination.getRows()));
        } catch (UIException e) {
            throw new AjaxException("获取事故列表信息失败！");
        }
        return "user/list";
    }

    @RequestMapping("/ajax/page")
    @ResponseBody
    public Object ajaxQuery(User user,
                            @RequestParam(required = false, defaultValue = "10") Integer rows,
                            @RequestParam(required = false, defaultValue = "1") Integer page, Model model) {
        try {
            return clientMap().success().pagination(userService.pageQuery(user, page, rows)).getMap();
        } catch (UIException e) {
            throw new AjaxException("获取用户列表信息失败！");
        }
    }

    @RequestMapping("/add/save")
    @ResponseBody
    public Map<String, Object> addSave(User user) {
        if (user == null) {
            throw new AjaxException("保存信息不能为空！");
        }
        try{
            if (userService.findUserByLoginId(user.getLoginId())!=null){
                throw new AjaxException("用户loginID已存在!");
            }
            user.setId(RandomUtil.getUUID());
           //user.setType(CONST.DEFAULT_USER_TYPE);
            user.setStatus(CONST.OBJECT_USED);
            user.setPasswordStart(new Date());
            //user.setPasswordEnd(DatetimeUtils.stringToDate(CONST.DEFAULT_PASSWORD_END,"yyyy-MM-dd HH:mm:ss"));
            user.setPasswordModify(CONST.OBJECT_UNUSED);
            userService.saveUser(user);
            return clientMap().success().message("保存成功！").getMap();
        }catch (Exception e) {
            throw new AjaxException(e.getMessage());
        }
    }

    @RequestMapping("/ajax/edit")
    @ResponseBody
    public Object ajaxQueryById(@RequestParam(required=true)String userid) {
        try {
            User user =  userService.findUserById(userid);
            return clientMap().success().datas(user).getMap();
        } catch (UIException e) {
            throw new AjaxException("获取用户信息失败！");
        }
    }
    /*附加的其他业务跳转*/
}
